package slice

func Map[T any, K any](s []T, proc func(v T, i int) K) []K {
	res := make([]K, len(s), cap(s))
	//length := len(s)
	//res := make([]K, length, length+length/4)
	for i, v := range s {
		res[i] = proc(v, i)
	}
	return res
}

func Has[T any](s []T, equal func(T) bool) bool {
	for _, v := range s {
		if equal(v) {
			return true
		}
	}
	return false
}
func Find[T any](s []T, predicate func(T) bool) (int, T) {
	for i, v := range s {
		if predicate(v) {
			return i, v
		}
	}
	var res T
	return -1, res
}

func Maps[T any, K any](proc func(v ...T) K, s ...[]T) []K {
	return nil
}

func Reduce[T any, K any](vals []T, init K, accumulator func(acc K, cur T) K) K {
	res := init
	for _, v := range vals {
		res = accumulator(res, v)
	}
	return res
}

func Filter[T any](vals []T, predicate func(v T) bool) []T {
	if vals == nil {
		return make([]T, 0)
	}
	//res := make([]T, len(vals), cap(vals))
	res := make([]T, len(vals))
	i := 0
	for _, v := range vals {
		if predicate(v) {
			res[i] = v
			i++
		}
	}
	res = res[:i]
	res = Shrink(res)
	return res
}

// 没有一个不满足为true
func Every[T any](vals []T, predicate func(s T) bool) bool {
	for _, p := range vals {
		if !predicate(p) {
			return false
		}
	}
	return true
}

// 至少一个满足为true
func Any[T any](vals []T, predicate func(s T) bool) bool {
	for _, v := range vals {
		if predicate(v) {
			return true
		}
	}
	return false
}

func Foreach[T any](vals []T, proc func(v T)) {
	for _, v := range vals {
		proc(v)
	}
}
